<template>
  <div :id="id" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
export default {
  props: {
    id: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "400px",
    },
    data: {
      type: Array,
      default: () => [],
    },
    title: {
      type: String,
      default: "用户活跃度",
    },
  },
  data() {
    return {
      chart: null,
      legendData: [],
      echartData: [],
      resizeTimer:null,
    };
  },
  mounted() {
    this.initChart();
    window.addEventListener('resize',this.erchartResize)
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart = null;
  },
  methods: {
    erchartResize() {
      if (this.resizeTimer) clearTimeout(this.resizeTimer);
      this.resizeTimer = setTimeout(()=>{
        this.chart.resize();
      }, 100)
    },
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id));
      const option = {
        title: {
          text: this.title,
          x: "center",
          textStyle: {
            color: "#1890ff",
          },
        },
        tooltip: {
          trigger: "item",
          formatter: "{b}日{c}度",
        },
        xAxis: {
          type: "category",
          data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
          axisLine: {
            lineStyle: {
              color: "#1890ff",
            },
          },
          splitLine: {
            show: false,
            lineStyle: {
                color: '#F3F4F4'
            }
        },
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true,
        },
        yAxis: {
          axisLine: {
            lineStyle: {
              color: "#1890ff",
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
                color: '#F3F4F4'
            }
        },
        },
        series: [
          {
            data: [12, 14, 15, 33, 45, 67, 95, 12, 56, 11, 23],
            type: "line",
            itemStyle: {
              normal: {
                color: "#1890ff",
                lineStyle: {
                  color: "#1890ff",
                  width: 1,
                },
              },
            },
          },
        ],
      };
      this.chart.setOption(option);
    },
  },
  destroyed () {
    window.removeEventListener('resize', this.erchartResize);
  },
};
</script>
<style lang='stylus' scoped>
.chartsClass {
  padding-left: 1.2rem;
}
</style>
</script>